﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SHJunketingModel;
using SHJunketingBLL;
using System.Collections.Generic;

public partial class Admin_liutao_UserRoleInfos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            //绑定数据
            DataView ldvlist = new DataView();
            ldvlist.Table = C_UserRoleInfoManager.GetUserRoleInfo(string.Empty);
            AspNetPager1.RecordCount = ldvlist.Table.Rows.Count;
            Session["dvlist"] = ldvlist;
            Session["roleName"] = string.Empty;
            bindData();
        }
    }

    #region 数据绑定
    /// <summary>
    /// 数据绑定
    /// </summary>    
    void bindData()
    {
        PagedDataSource pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = AspNetPager1.PageSize;
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        pds.DataSource = (DataView)Session["dvlist"];
        Repeater1.DataSource = pds;
        Repeater1.DataBind();
    }
    #endregion

    protected void btnsearch_Click(object sender, EventArgs e)
    {
        Web.CommFun.HasSqlInjectionChar(this);
        Session["roleName"] = tbsearch.Text.Trim();
        DataView ldvlist = new DataView();
        ldvlist.Table = C_UserRoleInfoManager.GetUserRoleInfo(Session["roleName"].ToString().Trim());
        AspNetPager1.RecordCount = ldvlist.Table.Rows.Count;
        Session["dvlist"] = ldvlist;
        bindData();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindData();
    }
    //添加角色
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddRoleInfo.aspx");
    }
    //删除角色
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        //操作
        if (Request.Form["roleId"] != null)
        {
            string id = Request.Form["roleId"];

            if (C_UserRoleInfoManager.DeleteC_UserRoleInfoByRoleId(id) != -1)
            {
               
                DataView ldvlist = new DataView();
                ldvlist.Table = C_UserRoleInfoManager.GetUserRoleInfo(Session["roleName"].ToString().Trim());
                AspNetPager1.RecordCount = ldvlist.Table.Rows.Count;
                Session["dvlist"] = ldvlist;
                bindData();
            }
        }
        else
        {
            Web.CommFun.Show_Msg("没有操作对象！", "UserRoleInfos.aspx");
        }    
    }


    public string GetPermissionsName(int roleId) 
    {
        string roleName = "";
        C_UserRoleInfo userRoleInfo = C_UserRoleInfoManager.GetC_UserRoleInfoByRoleId(roleId);
        string permission = userRoleInfo.Permissions;
        string sa = permission.Insert(0, "|");

        IList<C_Permission> list = C_PermissionManage.GetAllC_Permissions();

        foreach (C_Permission per in list)
        {
            int index = sa.IndexOf("|" + per.PermissionsID + "|");
            if (index != -1)
            {
                roleName += per.PermissionsName + "  ";
            }
        }
        return roleName;
    }
}
